package com.edu.edumanager.utils;

import ch.qos.logback.core.util.FileUtil;
import com.edu.edumanager.constant.EduConstant;
import com.edu.edumanager.exception.EduException;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import ws.schild.jave.MultimediaObject;
import ws.schild.jave.info.MultimediaInfo;

import java.io.File;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-02 22:42
 **/
public class EduUploaderUtils {
    /**
     * 获取到按照日期生成的文件名file
     * 类型
     * @return
     */
    public static File getRootFolder() {
        /**
         * 获取图片根路径
         * @return 返回图片服务器的根路径
         */
        File filePath = new File(EduConstant.IMAGEFILE);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateFolder = simpleDateFormat.format(date);
        return new File(filePath, dateFolder);
    }

    public static String getImageName(String fileName) {
        String newName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        return newName;
    }

    /**
     * @return 得到图片服务器的当前文件夹
     */
    public static String getDateFolder() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateFolder = format.format(date);
        return dateFolder;
    }

    /**
     *
     * @param fileName
     * @return 字符串为空 视频格式不合法
     */
    public static String getFilmSuffix(String fileName) {
        fileName = fileName.toLowerCase();
        if (fileName.isEmpty()) {
            return null;
        }
        if (fileName.endsWith(".avi")) {
            return ".avi";
        }
        if (fileName.endsWith(".mp4")) {
            return ".mp4";
        }
        if (fileName.endsWith(".mov")) {
            return ".mov";
        }
        if (fileName.endsWith(".wmv")) {
            return ".wmv";
        }
        if (fileName.endsWith(".flv")) {
            return ".flv";
        }
        if (fileName.endsWith(".MPEG")) {
            return ".MPEG";
        }
        return null;
    }

    public static void checkFilmFile(MultipartFile multipartFile){
        //判断格式
        int flagSize = 0;
        String suffix = EduUploaderUtils.getFilmSuffix(multipartFile.getOriginalFilename());
        if (suffix==null) {
            throw new EduException("上传格式错误,请选择后缀为avi,mp4,mov的视频文件");
        }
        try {
            InputStream inputStream = multipartFile.getInputStream();
            if (inputStream.available() > EduConstant.MAX_FILM_SIZE) {
                flagSize = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (flagSize == 1) {
            throw new EduException("文件大小不能超过5GB");
        }
    }

    public  static Long getVideoTime(File file){
        // 得到一个同名的临时文件
        // File file = new File(multipartFile.getOriginalFilename());
        try {
            // 把入参文件作为流 复制到 同名临时文件里
            // FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
            MultimediaObject instance = new MultimediaObject(file);
            MultimediaInfo result = instance.getInfo();
            Long videoTime = result.getDuration()/1000;
            // 所以最后临时文件要删除
            // file.delete();
            return  videoTime ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     *
     * @param time
     * @return
     */
    public  static String secTimeToString(Long time) {
        String timeStr = null;
        Long hour = 0L;
        Long minute = 0L;
        Long second = 0L;
        if (time <= 0){
            return "00:00";}
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99){
                    return "99:59:59";}
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }



    public static  String unitFormat(Long i) {
        String retStr = null;
        if (i >= 0 && i < 10){
            retStr = "0" + Long.toString(i);}
        else{
            retStr = "" + i;}
        return retStr;
    }
}
